#!/usr/bin/env bash
###
# 构建部署包
###
# set -x
set -eo pipefail

cd $(dirname $0)
SHELL_FOLDER=$(pwd)
WORK_DIR=$(cd .. && pwd)

echo "Work directory: $WORK_DIR"
ProjectName=$(basename $SHELL_FOLDER)

# 通过参数指定version
if [ $# -gt 0 ];then
    version=$1
else
    # version=dev-$(date +%Y-%m-%d-%H)
    version=latest
fi

cd $SHELL_FOLDER
file_release_info=current_revision
> $file_release_info
echo "Write package info to $file_release_info"
echo "pub time: "$(date '+%Y-%m-%d %H:%M:%S') >> $file_release_info
echo "commit id: $(git rev-parse HEAD)" >> $file_release_info

cd $WORK_DIR
package=${ProjectName}-${version}.tar.gz

if [ -f $package ]; then
    rm -f $package
fi
echo "Start to package $package"
gtar zcf $package \
    --exclude=${ProjectName}/.git \
    ${ProjectName}

mv $WORK_DIR/$package $SHELL_FOLDER/
cd $SHELL_FOLDER

ls -alh $package

rm -f current_revision
echo "Package $ProjectName done "`date +%Y-%m-%d-%H:%M:%S`
